function Sede (nombre, latitud, longitud, zoom) {
    this.nombre = nombre;
    this.latitud = latitud;
    this.longitud = longitud;
    this.zoom = zoom;
    this.getY = function(){
        return this.latitud;
    };
    this.getX = function(){
        return this.longitud;
    };
    this.getZoom = function(){
        return this.zoom;
    };
}

var Sartenejas = new Sede("Sartenejas", 10.410368, -66.882255, 16);
var Litoral = new Sede("Litoral", 10.618335, -66.718114, 17);

var map;
var infowindow = new google.maps.InfoWindow();
var edificios = [];
var robos = "";
var icons = ['style/images/Mapas/edificio4.gif','style/images/Mapas/edificio3.gif'];
/* Onload Actions
 *******************************************************************************/
$.(function(){
    initialize();
});

/* Functions
 *******************************************************************************/
function initialize(s) {
    if (typeof(window[s]) === "object") {
        sede = window[s];

        var latlng = new google.maps.LatLng(sede.getY() , sede.getX());
        var myOptions = {
            zoom: sede.getZoom(),
            draggable: true,
            scrollwheel: false,
            center: latlng,
            disableDefaultUI: true,
            navigationControl: true,
            navigationControlOptions: {
                style: google.maps.NavigationControlStyle.SMALL
            },
            mapTypeControl: true,
            mapTypeControlOptions: {
                style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
            },
            mapTypeId: google.maps.MapTypeId.HYBRID
        };
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    }
}

function getEspacios(){
    $.getJSON("listEspacios.do",function(data){
        if(!(espacios = data.espacios)) return;
        $.each(espacios, function(index, value){
            createMarker(value.nombre,value.coordX,value.coordY,0)
        });
    });
}

function createMarker(tittle, coordX, coordY, icon){
    icon = icons[icon];
    latlng = new google.maps.LatLng(coordY,coordX);
    var marker = new google.maps.Marker({
        map: map,
        position: latlng,
        title: tittle,
        icon: icon
    });
    var imagen = tittle.replace(/ /g, "");
    google.maps.event.addListener(marker, 'click', function() {
        content = "<div id='spaceTittle'>"+tittle+"</div>";
        content += "<div id='spaceImg'><img src='style/images/Espacios/"+imagen+".jpg'/></div>";
        content += "<div id='pisos'></div>";
        $('#spaceInfo').html(content);
        getPisos(tittle);
    });
    if(icon>0) edificios.push(marker);
}

// Removes the overlays from the map, but keeps them in the array
function clearEdificios() {
    arreglo = edificios;
    if (arreglo) {
        for (i in arreglo) {
            arreglo[i].setMap(null);
        }
    }
}

// Shows any overlays currently in the array
function showOverlays() {
    arreglo = edificios;
    if (arreglo) {
        for (i in arreglo) {
            arreglo[i].setMap(map);
        }
    }
}

function getUbications(){
    google.maps.event.addListener(map, 'click', function(event) {
        latlng = new google.maps.LatLng(event.latLng.lng(),event.latLng.lat());
        new google.maps.Marker({
            map: map,
            position: latlng
        });
        $('#coordX').val(event.latLng.lng());
        $('#coordY').val(event.latLng.lat());
    });
}
